package com.igeek.ssm.service;

import com.igeek.ssm.mapper.UserMapper;
import com.igeek.ssm.entity.User;
import com.igeek.ssm.entity.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO
 * @Author chenmin
 * @Version 1.0
 * @Date 2023/2/9 15:25
 */
@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    //登录
    @Transactional(readOnly = true)
    public User login(String name, String pwd) {
        //封装查询条件
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andPwdEqualTo(pwd);
        //执行查询
        List<User> users = userMapper.selectByExample(userExample);
        return users.size()>0?users.get(0):null;
    }
    //查看用户列表
    @Transactional(readOnly = true)
    public List<User> findAll(){
        List<User> users = userMapper.selectByExample(null);
        return users;
    }


    @Transactional(readOnly = true)
    public boolean register(User user){

        int i=userMapper.insertSelective(user);

        return i>0?true:false;

    }

}
